package com.songlanyun.modules.schedule;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.songlanyun.common.enums.IntegralConstant;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.advconfig.dao.AdvConfigDao;
import com.songlanyun.modules.advconfig.entity.AdvConfig;
import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointconfig.service.PointConfigService;
import com.songlanyun.modules.usergains.service.UserBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.util.List;

/**
 * 定时任务
 * <p>
 *
 */
@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class ClearAdvertisingRecords {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AdvConfigDao advConfigDao;

    @Autowired
    private IntegralRecordService integralRecordService;

    @Autowired
    private UserIdentityAssetService userIdentityAssetService;

    /**
     *清空用户每天观看广告记录，平分zydc奖励
     */
//    @Async
//    @Scheduled(cron = "${task0h10m}")
//    public void advUserRecord() {
//        log.info("清空用户每天观看广告记录");
//        //查询看满广告的用户
//        AdvConfig advConfig = advConfigDao.selectOne(new LambdaQueryWrapper<AdvConfig>().orderByDesc(AdvConfig::getId).last("LIMIT 1"));
//        if(advConfig!=null){
//            //满足平分条件的用户
//            List<AccountEntity> accountEntityList = accountService.selectUserAdvList(advConfig.getSeeTimesOneDay());
//            //总共的zydc
//            BigDecimal totalZydc = advConfig.getZydc();
//            //平分zydc(每人可获得的zydc数量)
//            BigDecimal divide = totalZydc.divide(new BigDecimal(accountEntityList.size()), 8, BigDecimal.ROUND_HALF_UP);
//
//            for (AccountEntity accountEntity : accountEntityList) {
//                //查询该用户资产
//                UserIdentityAssetEntity userIdentityAssetEntity = userIdentityAssetService.getById(accountEntity.getId());
//                //用户当前zydc值
//                BigDecimal integralAble = userIdentityAssetEntity.getIntegralAble();
//                //添加奖励后的zydc值
//                BigDecimal lastIntegralAble = integralAble.add(divide).setScale(8,BigDecimal.ROUND_HALF_UP);
//                userIdentityAssetEntity.setIntegralAble(lastIntegralAble);
//                //更新用户资产
//                userIdentityAssetService.updateById(userIdentityAssetEntity);
//                //添加资产记录
//                IntegralRecord integralRecord = new IntegralRecord(accountEntity.getId(), divide, IntegralConstant.InnerMode.ADVERTISING_REWARDS,
//                        IntegralConstant.IntegralModelType.ADVERTISING_REWARDS, accountEntity.getId(), "广告奖励");
//                integralRecordService.inner(integralRecord);
//            }
//
//            //获得奖励之后，清空观看记录
//            accountService.update(new UpdateWrapper<AccountEntity>().set("see_times", 0));
//        }
//    }


}
